package by.demchenko.sample.webapp.page.home.share;

import by.demchenko.sample.services.AccountService;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
import org.apache.wicket.validation.validator.StringValidator;

import javax.inject.Inject;


public class UniqueLoginValidator implements IValidator<String> {

    @Inject
    AccountService accountService;

    public UniqueLoginValidator() {
        Injector.get().inject(this);
    }

    @Override
    public void validate(IValidatable<String> validatable) {
        if (accountService.findLogin(validatable.getValue())) {
            ValidationError error = new ValidationError(this);
            error.setMessage("Логин " + validatable.getValue() + " не уникален!");
            validatable.error(error);
        }
    }
}
